﻿using System;
using System.Web.UI.WebControls;
using Negocio.Admin;
using Negocio.Auxiliar;


namespace qbaproject.Admin.Form
{
    public partial class formAdmin : System.Web.UI.Page
    {
        private int paginaAtual = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                carregarDdl();
            }
        }

        public void carregarDdl()
        {
            var obNStatus = new NStatus();
            var obNTipoUsuario = new NTipoUsuario();

            //Carregar status
            ddlStatus.DataSource = obNStatus.pegarTodosStatus();
            ddlStatus.DataBind();
            ddlStatus.Items.Insert(0, new ListItem("Selecionar o Status", "0"));

            //Carregar tipo usuario
            ddlTipoUsuario.DataSource = obNTipoUsuario.pegarTodosTiposUsuarios();
            ddlTipoUsuario.DataBind();
            ddlTipoUsuario.Items.Insert(0, new ListItem("Selecionar o Tipo Usuário", "0"));

        }

        protected void lkbPesquisar_Click(object sender, EventArgs e)
        {
            carregarGrid();
        }

        public void carregarGrid()
        {
            var csStatus = Convert.ToInt32(ddlStatus.SelectedValue);
            var obNAdministrador = new NAdministrador();
            var obListAdministrador = obNAdministrador.pegarAdministradores(txtNmAdministrador.Text, txtEmail.Text, csStatus);

            if (obListAdministrador.Tables[0].Rows.Count != 0)
            {
                divMsnPesquisaSucesso.Visible = true;
                lblNumEncontrados.Text = "Foram encontrados " + obListAdministrador.Tables[0].Rows.Count.ToString() + " registros.";
                divMsnPesquisaAtencao.Visible = false;
            }
            else
            {
                divMsnPesquisaSucesso.Visible = false;
                divMsnPesquisaAtencao.Visible = true;
            }

            grdResAdmin.PageSize = 15;
            grdResAdmin.AllowPaging = true;
            grdResAdmin.PageIndex = paginaAtual;
            grdResAdmin.DataSource = obListAdministrador;
            grdResAdmin.DataBind();
        }

        protected void grdResAdmin_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            paginaAtual = e.NewPageIndex;
            carregarGrid();
        }

        protected void lkbSim_Click(object sender, EventArgs e)
        {
            var idAdministrador = Convert.ToInt32(hdfIdAdministradorRemover.Value);
            var obNAdministrador = new NAdministrador();
            obNAdministrador.Remover(idAdministrador);
            carregarGrid();
        }

        protected void lkbNao_Click(object sender, EventArgs e)
        {
            ModalPopupRemover.Hide();
        }

        protected void grdResAdmin_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "editar":
                    var idAdministrador = e.CommandArgument.ToString();
                    Response.Redirect("~/Admin/Editar/editarAdmin.aspx?id=" + idAdministrador);
                    break;
                case "remover":
                    var idAdministradorRemover = e.CommandArgument.ToString();
                    hdfIdAdministradorRemover.Value = idAdministradorRemover;
                    ModalPopupRemover.Show();
                    break;
            }

        }

        protected void lnk_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/Incluir/incluirAdmin.aspx");
        }

        protected void grdResAdmin_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                var obNStatus = new NStatus();
                obNStatus.pegarStatusPorId(Convert.ToInt32(e.Row.Cells[3].Text));
                e.Row.Cells[3].Text = obNStatus.dsStatus;

                var obNTipoUsuario = new NTipoUsuario();
                obNTipoUsuario.pegarTipoUsuarioPorId(Convert.ToInt32(e.Row.Cells[2].Text));
                e.Row.Cells[2].Text = obNTipoUsuario.dsTipoUsuario;
            }
        }
    }
}